package view;

import java.awt.Adjustable;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.SystemColor;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import model.Kalender;

/**
 * @uthor BWH
 */

public class GUI_Kalender extends JPanel {
	private final JTable table;
	JScrollBar scrollBar;
	TeraFootMain teraFootMain;
	JLabel lblUge;
	JComboBox comboBox;
	JTextField textAntal;
	JButton btnSletTid;
	JLabel lblKommentarFelt;
	JTextArea textComment;
	JButton btnGemKommentar;
	JButton btnBookTid;
	JComboBox comboBoxUge;
	JLabel lblStatus;
	private final JLabel lblKommentarFor;

	/**
	 * Create the panel.
	 */
	public GUI_Kalender(TeraFootMain teraFootMain) {
		setBackground(SystemColor.activeCaption);
		this.teraFootMain = teraFootMain;
		setLayout(null);
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(68, 71, 362, 264);
		add(scrollPane);

		final Calendar cal = Calendar.getInstance();

		table = new JTable();
		table.setColumnSelectionAllowed(true);
		table.setToolTipText("Klik p\u00E5 knapperne for neden for at Booke eller Slette den valgte tid");
		table.setRowSelectionAllowed(true);
		table.setCellSelectionEnabled(true);
		scrollPane.setViewportView(table);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getColumnModel().getSelectionModel()
				.addListSelectionListener(teraFootMain.getSelectListener());
		table.getSelectionModel().addListSelectionListener(
				teraFootMain.getSelectListener());
		table.setBackground(SystemColor.activeCaptionBorder);
		refreshTable(cal.get(Calendar.YEAR), cal.get(Calendar.WEEK_OF_YEAR));
		table.setDefaultRenderer(Object.class,
				new AlternateTableRowColorRenderer());

		comboBox = new JComboBox();
		comboBox.setToolTipText("\u00C5r");
		comboBox.setActionCommand("comboBoxKal");
		comboBox.addActionListener(teraFootMain.getAcListener());
		comboBox.setBounds(352, 38, 75, 20);
		final int year = cal.get(Calendar.YEAR);
		final String[] years = new String[5];
		for (int i = 0; i < years.length; i++) {
			years[i] = "" + (year + i);
		}

		comboBox.setModel(new DefaultComboBoxModel(years));
		comboBox.setSelectedItem(cal.get(Calendar.YEAR));
		add(comboBox);

		lblUge = new JLabel("UGE:");
		lblUge.setBounds(315, 12, 39, 17);
		add(lblUge);
		scrollBar = new JScrollBar();
		scrollBar.setBounds(70, 38, 272, 20);

		scrollBar.setValue(cal.get(Calendar.WEEK_OF_YEAR));
		scrollBar.setMinimum(1);
		scrollBar.setMaximum(62);
		scrollBar.setOrientation(Adjustable.HORIZONTAL);
		scrollBar.addAdjustmentListener(teraFootMain.getAdjustListener());
		scrollBar.setToolTipText("" + scrollBar.getValue());
		add(scrollBar);

		btnBookTid = new JButton("Book tid");
		btnBookTid.setBounds(68, 346, 89, 23);
		btnBookTid.setToolTipText("Book valgte tid p\u00E5 tabelen");
		btnBookTid.setActionCommand("Book tid");
		btnBookTid.addActionListener(teraFootMain.getAcListener());
		add(btnBookTid);

		textAntal = new JTextField();
		textAntal.setBounds(400, 381, 30, 20);
		textAntal.setHorizontalAlignment(SwingConstants.CENTER);
		textAntal.setText("4");
		textAntal.setToolTipText("Max reservationer pr tid.");
		textAntal.setActionCommand("antalChanged");
		textAntal.addActionListener(teraFootMain.getAcListener());
		add(textAntal);
		textAntal.setColumns(10);

		final JLabel lblMaxPatienterAd = new JLabel(
				"Max patienter til hver tid:");
		lblMaxPatienterAd.setBounds(233, 380, 167, 23);
		add(lblMaxPatienterAd);

		btnSletTid = new JButton("Slet tid");
		btnSletTid.setBounds(68, 380, 89, 23);
		btnSletTid.setToolTipText("Slet en tidsbestilling p\u00E5 tabellen");
		btnSletTid.setActionCommand("SletTid");
		btnSletTid.addActionListener(teraFootMain.getAcListener());
		add(btnSletTid);

		final JLabel lblRd = new JLabel("For mange tider reserverede");
		lblRd.setBounds(437, 10, 168, 20);
		lblRd.setOpaque(true);
		lblRd.setForeground(Color.BLACK);
		lblRd.setBackground(Color.RED);
		add(lblRd);

		final JLabel lblIngenTiderTilbage = new JLabel("Fuldt booket");
		lblIngenTiderTilbage.setBounds(437, 38, 168, 20);
		lblIngenTiderTilbage.setOpaque(true);
		lblIngenTiderTilbage.setBackground(Color.ORANGE);
		add(lblIngenTiderTilbage);

		final JLabel lblnTidTilbage = new JLabel("\u00C9n tid tilbage");
		lblnTidTilbage.setBounds(437, 69, 168, 20);
		lblnTidTilbage.setOpaque(true);
		lblnTidTilbage.setBackground(Color.YELLOW);
		add(lblnTidTilbage);

		final JLabel lblMereEndEn = new JLabel("Mere end en tid tilbage");
		lblMereEndEn.setBounds(437, 100, 168, 20);
		lblMereEndEn.setOpaque(true);
		lblMereEndEn.setBackground(Color.GREEN);
		add(lblMereEndEn);

		final JLabel lblNewLabel = new JLabel(
				"OBS! Kan tage 10-15 sekunder at oprette en ny uge!");
		lblNewLabel
				.setToolTipText("Hvis ugen ikke findes bliver den oprettet i databasen. Det tager normalt mellem 10-15 sekunder");
		lblNewLabel.setBounds(68, 24, 376, 14);
		lblNewLabel.setFont(new Font("Tahoma", Font.ITALIC, 11));
		add(lblNewLabel);

		textComment = new JTextArea();
		textComment
				.setToolTipText("\u00C6ndr kommentar her for den valgte tid p\u00E5 listen og klik \"Gem Kommentar\" for at gemme den");
		textComment.setBounds(440, 163, 165, 172);
		textComment.setLineWrap(true);
		add(textComment);

		lblKommentarFelt = new JLabel("");
		lblKommentarFelt.setBounds(440, 138, 165, 14);
		add(lblKommentarFelt);

		btnGemKommentar = new JButton("Gem kommentar");
		btnGemKommentar.setActionCommand("GemKommentar");
		btnGemKommentar.addActionListener(teraFootMain.getAcListener());
		btnGemKommentar.setBounds(437, 362, 168, 39);
		add(btnGemKommentar);
		add(textAntal);

		comboBoxUge = new JComboBox();
		final String[] uger = new String[52];
		for (int uge = 1; uge <= 52; uge++) {
			uger[uge - 1] = "" + uge;
		}
		comboBoxUge.setModel(new DefaultComboBoxModel(uger));
		comboBoxUge.setSelectedIndex(cal.get(Calendar.WEEK_OF_YEAR) - 1);
		comboBoxUge.setActionCommand("comboUge");
		comboBoxUge.addActionListener(teraFootMain.getAcListener());
		comboBoxUge.setToolTipText("Uge");
		comboBoxUge.setBounds(352, 10, 75, 20);
		add(comboBoxUge);

		final JLabel lblSkifteUgeEnten = new JLabel(
				"V\u00E6lg uge, enten med scrollbar eller dropbox");
		lblSkifteUgeEnten.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblSkifteUgeEnten.setBounds(68, 10, 266, 17);
		add(lblSkifteUgeEnten);

		lblStatus = new JLabel("");
		lblStatus.setBackground(SystemColor.activeCaption);
		lblStatus.setOpaque(true);
		lblStatus.setBounds(167, 350, 263, 14);
		add(lblStatus);

		lblKommentarFor = new JLabel("Kommentar for:");
		lblKommentarFor.setBounds(440, 119, 136, 14);
		add(lblKommentarFor);

	}

	public void refreshTable(int year, int week) {
		//et check om ugen eksister, ellers bliver den lavet her
		teraFootMain.getDbConnection().checkIfWeekExists(year, week);
		final ArrayList<Kalender> kalender = teraFootMain.getDbConnection()
				.getWeek(year, week);
		final String[][] array = new String[kalender.size()][6];
		int i = 0;
		for (final Kalender calen : kalender) {
			array[i][0] = calen.getKlokken();
			array[i][1] = calen.getMandag();
			array[i][2] = calen.getTirsdag();
			array[i][3] = calen.getOnsdag();
			array[i][4] = calen.getTorsdag();
			array[i][5] = calen.getFredag();

			i++;
		}
		table.setModel(new DefaultTableModel(array, new String[] { "Klokken",
				"Mandag", "Tirsdag", "Onsdag", "Torsdag", "Fredag" }

		) {
			Class[] columnTypes = new Class[] { String.class, String.class,
					String.class, String.class, String.class, String.class };

			@Override
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false, false };

			@Override
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});

		System.out.println("Kalender table refreshed");
	}

	class AlternateTableRowColorRenderer extends DefaultTableCellRenderer {

		Color currColor = Color.white;

		public AlternateTableRowColorRenderer() {
			setOpaque(true);
		}

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {

			if (row % 2 == 0) {
				currColor = Color.white;
			} else {
				currColor = new Color(222, 222, 255);
			}

			if (column != 0) {
				if (Integer.parseInt("" + table.getValueAt(row, column)) > Integer
						.parseInt(textAntal.getText())) {
					currColor = Color.RED;
				}

				else if (Integer.parseInt("" + table.getValueAt(row, column)) == Integer
						.parseInt(textAntal.getText())) {
					currColor = Color.ORANGE;
				}

				else if (Integer.parseInt("" + table.getValueAt(row, column)) == Integer
						.parseInt(textAntal.getText()) - 1) {
					currColor = Color.YELLOW;
				}

				else if (Integer.parseInt("" + table.getValueAt(row, column)) < Integer
						.parseInt(textAntal.getText()) - 1) {
					currColor = Color.GREEN;
				}
			}
			if (isSelected) {
				super.setForeground(table.getSelectionForeground());
				super.setBackground(table.getSelectionBackground());
			} else {
				super.setForeground(Color.black);
				super.setBackground(currColor);
			}
			setFont(table.getFont());
			setValue(value);

			return this;
		}
	}

	public JLabel getLblUge() {
		return lblUge;
	}

	public JComboBox getComboBox() {
		return comboBox;
	}

	public JScrollBar getScrollBar() {
		return scrollBar;
	}

	public JTable getTable() {
		return table;
	}

	public JTextField getTextAntal() {
		return textAntal;
	}

	public JTextArea getTextComment() {
		return textComment;
	}

	public JButton getBtnTestKomment() {
		return btnGemKommentar;
	}

	public JComboBox getComboBoxUge() {
		return comboBoxUge;
	}

	public JLabel getLblStatus() {
		return lblStatus;
	}

	public JLabel getLblKommentarFelt() {
		return lblKommentarFelt;
	}

}
